<?php
/*
 * Created on 30-04-2011
 */
 class HomeController extends AppController {

	var $uses = array('Survey', 'Emotion', 'Adjective', 'EmotionsSurvey', 'AdjectivesSurvey');
	var $helpers = array('Form');
	var $components = array('Email');
	function thanks(){

	}
 	function index(){
		$adjectives = $this->Adjective->find('list');
		$emotions = $this->Emotion->find('list');

		$this->set(compact('adjectives', 'emotions'));
		if(!empty($this->data)){
			$adjs = 0;
			foreach($this->data['Adjective'] as $aid => $val){
				if (!empty($val)) $adjs++;
			}

			$ems = 0;
			foreach($this->data['Emotion'] as $eid => $val){
				if (!empty($val)) $ems++;
			}

			if($adjs == count($this->data['Adjective']) || $ems == count($this->data['Emotion'])) {

				if ($this->Survey->save($this->data)){
					$sid = $this->Survey->getLastInsertID();

					$emotions = array();
					foreach($this->data['Emotion'] as $eid => $val){
						$emotions[] = array(
							'survey_id' => $sid,
							'emotion_id' => $eid,
							'value' => $val
						);
					}

					$this->EmotionsSurvey->saveAll($emotions);

					$adjv = array();
					foreach($this->data['Adjective'] as $aid => $val){
						$adjv[] = array(
							'survey_id' => $sid,
							'adjective_id' => $aid,
							'value' => $val
						);
					}
					$this->AdjectivesSurvey->saveAll($adjv);
					$this->redirect(array('action' => 'thanks'));
				}
			} else {
				$this->Session->setFlash('Należy wypełnić wszystkie pytania');
			}
		}
 	}

 	function send_ask(){
        $limitD = 20;
        $limitU = 42;
        $emails = ClassRegistry::init('Email');
        $responders = $this->Survey->find('list', array(
                'conditions' => array(
                        'id BETWEEN ? AND ?' => array($limitD, $limitU)
                ),
                'fields' => array('id', 'email')
        ));
        $emails = $emails->find('list', array(
                        'fields' => array('id', 'email'),
                        'conditions' => array('version' => 1),
                )
        );
        $emails = array_diff($emails, $responders);
        $emails = array('mabaranska@gmail.com');
        $responders = array('mabaranska@gmail.com');
        $this->Email->from = 'mabaranska@gmail.com';
        $this->Email->bcc = 'michalwski@gmail.com';
        $this->Email->subject = 'Bieg Trzech Kopców - ostatnia część badania dotyczącego psychologicznych aspektów bólu';
        $this->Email->template = 'ask';
        echo '<br/>';
        $i = 1;
        foreach($responders as $email){
                echo $i.' - '.$email.'<br/>';
                $this->Email->to = $email;
                $this->Email->send();
                $i++;
        }
        echo '<br/>';
        $this->Email->template = 'ask2';
        foreach($emails as $email){
                echo $i.' - '.$email.'<br/>';
                $this->Email->to = $email;
                $this->Email->send();
                $i++;
        }
    }


 	function stats($csv = false){
 		$this->Survey->bindModel(array(
 			'hasAndBelongsToMany' => array('Emotion' => array('order' => 'Emotion.id'), 'Adjective' => array('order' => 'Adjective.id'))
 		));
 		$stats = $this->Survey->find('all');
 		$this->set(compact('stats'));
 		if ($csv) {
 			header("Content-type: application/vnd.ms-excel; name='excel'");
			header("Content-Disposition: filename=export.csv");

			$this->layout = false;
			$this->render('stats_csv');
			$this->autoRender = false;
			return;
 		}
 	}
 }
?>
